<template>
    <div>
        <el-table max-height="510px" :data="specsList" border style="width: 100%">
            <el-table-column prop="id" label="规格编号" width="80">
            </el-table-column>
            <el-table-column prop="specsname" label="规格名称" width="180">
            </el-table-column>
            <el-table-column prop="attrs" label="规格属性">
                <template slot-scope="item">
                    <div>
                        <el-button v-for="attrs in item.row.attrs" :key="attrs" type="success"  plain size="mini">{{ attrs }}
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="状态">
                <template slot-scope="item">
                    <div>
                        <!-- {{ item.row.status == 1 ? '启用' : '禁用' }} -->
                        <el-tag v-if="item.row.status == 1">启用</el-tag>
                        <el-tag v-else>禁用</el-tag>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="item">
                    <div>
                        <el-button size="mini" @click="edit(item.row.id)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="del(item.row.id)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background layout="prev, pager, next" :page-size="getSize" :total="specsCount"
            @current-change="changePage">
        </el-pagination>
    </div>
</template>

<script>
// 辅助性函数
import { mapGetters, mapActions } from 'vuex'
// 引入接口
import { specsDel } from '../../request/api'
export default {
    data() {
        return {
        };
    },
    computed: {
        ...mapGetters({
            specsList: 'specs/getSpecsList',
            specsCount: 'specs/getCount',
            getSize: 'specs/getSize'
        })
    },
    mounted() {
        // 列表
        this.specsListAction(),
            // 总数
            this.specsCountAction()
    },
    methods: {
        ...mapActions({
            specsListAction: 'specs/changeSpecsAction',  //列表
            specsCountAction: 'specs/changeCountAction',  //总数
            changePageAction: 'specs/changePageAction'  // 页码
        }),
        //封装一个切换页码事件
        changePage(n) {
            this.changePageAction(n);
        },
        // 编辑事件
        edit(id) {
            this.$emit('edit',id)
        },
        // 删除事件
        del(id) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 调用接口
                specsDel({
                    id
                }).then(res => {
                    // console.log(res, '删除');
                    this.$message.error(res.msg)
                    // 删除成功，重新调用列表和总数
                    // 列表
                    this.specsListAction(),
                        // 总数
                        this.specsCountAction()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        }

    }
};
</script>

<style  lang="less" scoped>
.tag {
    margin-right: 3px;
}
</style>
